package com.project.nimsen.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.WxsendmsgDto;
import com.project.nimsen.entity.Wxsendmsg;
import com.project.nimsen.service.IWxsendmsgService;
import com.project.nimsen.util.NimsenUtil;
import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
@Repository("wxsendmsgService")
public class WxsendmsgServiceImpl extends BaseServiceImpl implements
		IWxsendmsgService {
	String allcol = "id,keyword,rtext,fileid,url,title,msgtype,pid,"
			+ "targetid,siteid,createUser,createTime,active";
	@Resource
	IBaseDao baseDao;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public Map<String, Object> findValidate(WxsendmsgDto dto) throws Exception {
		if(StringUtils.isBlank(dto.getValifield())||StringUtils.isBlank(dto.getValivalue()))
			return null;
		Finder finder = new Finder("SELECT "+allcol+" FROM wxsendmsg where "
			+ "active!=1");
		finder.append(" and ").append(dto.getValifield()).append("=:").append(dto.getValifield());
		finder.setParam(dto.getValifield(), dto.getValivalue());
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and id!=:vid");
			finder.setParam("vid", dto.getId());
		}
		if(StringUtils.isNotBlank(dto.getSiteid())){
			finder.append(" and siteid=:vsiteid");
			finder.setParam("vsiteid", dto.getSiteid());
		}
		return baseDao.queryForObject(finder);
	}
	public Wxsendmsg findByid(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return null;
		Finder finder = new Finder("SELECT "+allcol+" FROM wxsendmsg where active!=1 and id=:id");
		finder.setParam("id", id);
		return baseDao.queryForObject(finder, Wxsendmsg.class);
	}
	public boolean deleteByid(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return false;
		Wxsendmsg wxsmsg = findByid(id);
		if(wxsmsg==null)
			return false;
		//当有目标回复信息时
		if(StringUtils.isNotBlank(wxsmsg.getTargetid())){
			deleteByid(id);
		}
		List<Wxsendmsg> childs = findChildByPid(wxsmsg.getId());
		//判断是否有子信息
		if(!CollectionUtils.isEmpty(childs)){
//			for(Wxsendmsg msg : childs)
//				msg.setActive(1);
//			baseDao.updateList(childs);
			deleteChildById(id);
		}
		wxsmsg.setActive(1);
		baseDao.update(wxsmsg);
		return true;
	}
	private List<Wxsendmsg> findChildByPid(String pid)throws Exception{
		if(StringUtils.isBlank(pid))
			return null;
		Finder finder = new Finder("SELECT "+allcol+" FROM wxsendmsg where active!=1 and pid=:pid");
		finder.setParam("pid", pid);
		return baseDao.queryForList(finder, Wxsendmsg.class);
	}
	public List<Wxsendmsg> findEntByDto(WxsendmsgDto dto) throws Exception {
		Finder finder = finder(dto);
		return baseDao.queryForList(finder, Wxsendmsg.class, dto.getPage());
	}
	private Finder finder(WxsendmsgDto dto){
		Finder finder = new Finder("SELECT "+allcol+" FROM wxsendmsg where "
			+ "active!=1");
		if(StringUtils.isNotBlank(dto.getRtype())){
			finder.append(" and msgtype=:msgtype");
			finder.setParam("msgtype", dto.getRtype());
		}
		if(StringUtils.isNotBlank(dto.getSiteid())){
			finder.append(" and siteid=:siteid");
			finder.setParam("siteid", dto.getSiteid());
		}
		if(StringUtils.isNotBlank(dto.getPid())){
			finder.append(" and pid=:pid");
			finder.setParam("pid", dto.getPid());
		}else
			finder.append(" and (pid is null or pid='')");
		return finder;
	}
	public List<Map<String, Object>> findByDto(WxsendmsgDto dto) throws Exception {
		Finder finder = finder(dto);
		return baseDao.queryForList(finder, dto.getPage());
	}
	public boolean saveNews(WxsendmsgDto dto) throws Exception {
		Wxsendmsg news = dto.getNewsr();
		String id = news.getId();
		if(StringUtils.isNotBlank(id)){
			update(news);
			deleteChildById(id);
		}else{
			Object result = save(news);
			id = NimsenUtil.obj2String(result);
		}
		List<Wxsendmsg> childs = dto.getNews_childs();
		for(Wxsendmsg msg : childs)
			msg.setPid(id);
		save(childs);
		return true;
	}
	private void deleteChildById(String id)throws Exception{
		Finder finder = new Finder("update wxsendmsg set active=1 where active!=1 and pid=:pid");
		finder.setParam("pid", id);
		baseDao.update(finder);
	}
	public List<Map<String, Object>> findNewsMapByDto(WxsendmsgDto dto)
			throws Exception {
		Finder finder = new Finder("SELECT wx.id,wx.keyword,wx.rtext,wx.fileid,wx.url,wx.title,"
			+ "wx.msgtype,wx.pid,wx.targetid,wx.siteid,wx.createUser,wx.createTime,wx.active,fi."
			+ "filename FROM wxsendmsg as wx left outer join fileinfo as fi on wx.fileid=fi.id "
			+ "where wx.active!=1");
		if(StringUtils.isNotBlank(dto.getRtype())){
			finder.append(" and wx.msgtype=:msgtype");
			finder.setParam("msgtype", dto.getRtype());
		}
		if(StringUtils.isNotBlank(dto.getSiteid())){
			finder.append(" and wx.siteid=:siteid");
			finder.setParam("siteid", dto.getSiteid());
		}
		if(StringUtils.isNotBlank(dto.getPid())){
			finder.append(" and wx.pid=:pid");
			finder.setParam("pid", dto.getPid());
		}else
			finder.append(" and (pid is null or pid='')");
		finder.append(" order by wx.sort asc");
		return baseDao.queryForList(finder, dto.getPage());
	}
	public Wxsendmsg findByKeyWord(String keyword, String siteid) throws Exception {
		if(StringUtils.isBlank(keyword)||StringUtils.isBlank(siteid))
			return null;
		Finder finder = new Finder("SELECT "+allcol+" FROM wxsendmsg where active!=1 and keyword"
			+ "=:keyword and siteid=:siteid");
		finder.setParam("siteid", siteid);
		finder.setParam("keyword", keyword);
		return baseDao.queryForObject(finder, Wxsendmsg.class);
	}
}
